package com.dayou.handler;

import com.alibaba.fastjson.JSONObject;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class SendTest {
    static String borkerURL = "tcp://172.20.40.210:61616";
    static String testQueue = "bs.interviewer";

    public static void main(String[] args) {
        try {
            JSONObject jo = new JSONObject();
            jo.put("type", "room");
            jo.put("msgType", "room-voice");
            jo.put("msgBody", "请到");
            jo.put("moduleName", "meeting");
            String textMsg = jo.toJSONString();
            System.out.println(textMsg);
            send(textMsg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void send(String text)
            throws Exception {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(borkerURL);

        Connection connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(Boolean.TRUE.booleanValue(), 1);

        Destination destination = session.createQueue(testQueue);

        MessageProducer producer = session.createProducer(destination);

        producer.setDeliveryMode(2);

        TextMessage textMessage = session.createTextMessage(text);

        producer.send(textMessage);

        session.commit();
        session.close();
        connection.close();
    }
}
